package com.zzy.demo.action.upload;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.zzy.demo.dto.UploadFile;
import com.zzy.demo.utils.ImageReduce;
import com.zzy.demo.utils.UploadFileUtils;

/**
 * 
 * @author guokaige
 *
 */
@Controller
@RequestMapping("/simple")
public class SimpleUploadAction{
	
	
	 @RequestMapping("/index")
	 public String uploadIndex(HttpServletRequest request, ModelMap model) {

	    	//企业基本信息
	        return "/simple/upload";
	    }
	 
	 
	 @RequestMapping("/multipart")
	 @ResponseBody
	 public String uploadMultipart(HttpServletRequest request, ModelMap model ,
			                        @ModelAttribute UploadFile uploadFile) throws Exception {
		 
		    System.out.println(request.getParameter("username"));
		    
		    //a文件
		    MultipartFile  multipartFileA=uploadFile.getMultipartFileA();
		    
		    
		    String fileName=UploadFileUtils.createFileName(multipartFileA.getOriginalFilename());
			
			//获取跟目录
			File path = new File(ResourceUtils.getURL("classpath:").getPath());
			if(!path.exists()) path = new File("");
			String filePath = path.getAbsolutePath();
			
			//获取配置文件路径：
			String relativePath="/data";
			File upload = new File(filePath,relativePath);
			if(!upload.exists()) upload.mkdirs();
			String uploadPath=upload.getAbsolutePath();
			
			UploadFileUtils.inputStream2File(multipartFileA.getInputStream(),uploadPath,fileName);
			
			
			
			System.out.println("-------httppath--------"+request.getContextPath()+"----------------"+fileName);
			String httpPath=request.getContextPath()+File.separator+fileName;
			
	    	//企业基本信息
	        return httpPath;
	    }
	 
	 
	 /**
	  * 等比缩放
	  * @Title:        title
	  * @Description:  TODO(这里用一句话描述这个方法的作用) 
	  * @param multipartFile
	  * @return    
	  * @version       V2.0 
	  * @author       guokaige
	  * @Date          2017-2-7 下午2:23:19
	  */
	 public  byte[]  multipartReduce(MultipartFile  multipartFile){
		 //等比缩放
		    InputStream in = null;
			try {
				in = multipartFile.getInputStream();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
			byte[] byteImg = ImageReduce.getImgWHScale(in, 600, 600, 1, true);
			return byteImg;
	 }
	 
	 
	 
	 
	 
	 
	 /**
	  * byte[]转file
	  * @Title:        title
	  * @Description:  TODO(这里用一句话描述这个方法的作用) 
	  * @param buf
	  * @param filePath
	  * @param fileName    
	  * @version       V2.0 
	  * @author       guokaige
	  * @Date          2017-2-7 下午2:06:32
	  */
	 public  void byte2File(byte[] buf, String filePath, String fileName) {
			BufferedOutputStream bos = null;
			FileOutputStream fos = null;
			File file = null;
			try {
				File dir = new File(filePath);
				if (!dir.exists() && dir.isDirectory()) {
					boolean flag = dir.mkdirs();
					if (!flag) {
						System.out.println("创建父级目录失败！");
					}
				}
				file = new File(filePath + File.separator + fileName);
				fos = new FileOutputStream(file);
				bos = new BufferedOutputStream(fos);
				bos.write(buf);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (bos != null) {
					try {
						bos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fos != null) {
					try {
						fos.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
	 
	
}
